/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobiwebinc.compconn.announcer;

import com.apple.dnssd.DNSSD;
import com.apple.dnssd.DNSSDException;
import com.apple.dnssd.DNSSDRegistration;
import com.apple.dnssd.DNSSDService;
import com.apple.dnssd.RegisterListener;
import com.apple.dnssd.TXTRecord;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author suraj
 */
public class BonjourAnnouncer extends Announcer implements RegisterListener {
    TXTRecord record;
    DNSSDRegistration registration ;

    public BonjourAnnouncer() {
        record = new TXTRecord();
    }

    public void serviceRegistered(DNSSDRegistration registration, int flags, String serviceName, String regType, String domain) {
        System.out.println("successfully registered iconnector");
    }

    public void operationFailed(DNSSDService registration, int error) {
        System.err.println("failed to register iconnector");
    }

    @Override
    public void registerService(String name, int port) {
        try {
            registration = DNSSD.register(DNSSD.NO_AUTO_RENAME, DNSSD.ALL_INTERFACES, name, "_iconnector._tcp", null, null, port, record, this);
        } catch (DNSSDException ex) {
            Logger.getLogger(BonjourAnnouncer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void setParamters(String name, String value) {
        record.set(name, value);
    }

    @Override
    public void stop() {
        if(registration != null)
        registration.stop();
    }
}
